﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CamShared;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using DavuxLib;


namespace CamServer
{
    static class Program
    {
        
        static string[] args;
        static bool UI = false;

        [STAThread]
        static void Main(string[] args)
        {
            Program.args = args;
            Thread.CurrentThread.Name = "MAIN";
            try
            {
                if (args.Contains("--no-appdata"))
                {
                    Logging.Init("CamServer_Log.txt");
                }
                else
                {
                    Logging.InitAppData("CamServer_Log.txt");
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error starting Tracer: " + ex);
            }

            if (!args.Contains("--noui"))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                UI = true;
            }

            DavuxLib.Settings.SaveFileName = "CamServer_Settings.xml";
            DavuxLib.Settings.Load();

            DavuxLib.RoboRestarter.EnableHosting(args);

            if (UI && !DavuxLib.Registration.CanExecuteEx("CamServer"))
            {
                Trace.WriteLine("Registration Failed");
                MessageBox.Show("This software is not registered.", "CamServer Registration", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (!args.Contains("--no-mutex") && UI)
            {
                if (DavuxLib.Startup.AppAlreadyRunning("CamServer0"))
                {
                    if (MessageBox.Show("CamServer is already running.\n\nWould you like to continue anyway?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                    {
                        Trace.WriteLine("CamServer already running, closing.");
                        Environment.Exit(75);
                    }
                }
            }


            Trace.WriteLine("Starting CamServer");

            try
            {
                DavuxLib.Startup.RunAtStartup("CamServer", Settings.Get("Settings|Run At Windows Startup", false));
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error setting startup: " + ex);
            }

            //Settings.SetRestartRequired("Settings|Run At Windows Startup");

            try
            {
                if (UI)
                {
                    MainForm m = new MainForm();
                    Thread t = new Thread(Init);
                    t.Start();
                    Application.Run();
                }
                else
                {
                    Init();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error Executing CamHost: " + ex.ToString());
            }
        }


        static void sv_OnExit()
        {
            try
            {
                CamHost.Dispose();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error disposing CamHost: " + ex);
            }
            try
            {
                DavuxLib.Startup.CleanupMutex();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error disposing Mutex: " + ex);
            }
        }

        static void Init()
        {
            try
            {
                if (!CamHost.Initialize())
                {
                    Trace.WriteLine("Init Failed, bailing out");
                    if (UI)
                    {
                        MessageBox.Show("Unable to initialize CamHost, a plugin may be preventing this application from starting.  Consult the log file for more information", "CamServer error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        Trace.WriteLine("Unable to initialize CamHost, a plugin may be preventing this application from starting.  Consult the log file for more information");
                    }
                    Environment.Exit(5);
                }
                if (UI)
                {
                    Application.Run();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error in CamHost: " + ex.ToString());
            }
        }
    }
}
